// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Sign Post Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 20 : TYPE_SIGNPOST
private alias object.value7 : signpost.timeout

// Function declarations
reserve function SignPost_DebugDraw
reserve function SignPost_DebugSpawn

// Static Values

// Tables

function SignPost_DebugDraw
	DrawSprite(0)
	DrawSprite(1)
end function


function SignPost_DebugSpawn
	CreateTempObject(TypeName[Sign Post], 0, object.xpos, object.ypos)
	object[tempObjectPos].frame = 1
	object[tempObjectPos].value0 = 16
end function


event ObjectMain
		
	switch object.state
	case 0
		CheckEqual(object[0].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0		
			temp0 = object.ypos
			temp0 += 0x400000
			if object[0].ypos < temp0
				stage.newXBoundary1 = object.xpos
				stage.newXBoundary1 >>= 16
				stage.newXBoundary1 -= screen.xcenter
				stage.newXBoundary1 -= screen.xcenter
				stage.newXBoundary2 = object.xpos
				stage.newXBoundary2 >>= 16
				stage.newXBoundary2 += screen.xcenter
				if object[0].xpos > object.xpos
					stage.newXBoundary1 = object.xpos
					stage.newXBoundary1 >>= 16
					stage.newXBoundary1 -= screen.xcenter
					if stage.debugMode == 0
						if stage.listPos == 0
							if stage.minutes == 0
								if stage.seconds < 30
									achievement.trigger = 1
									//CallNativeFunction2(SetAchievement, 1, 100)
								end if
							end if
						end if
						if stage.listPos == 7
							temp0 = 230
							temp0 <<= 16
							if object[0].ypos < temp0
								achievement.trigger = 16
							end if
						end if
					end if
					if stage.timeEnabled == 1
						if options.gameMode == 2
							timeAttack.result = stage.seconds
							timeAttack.result *= 100
							temp0 = stage.minutes
							temp0 *= 0x1770
							timeAttack.result += temp0
							timeAttack.result += stage.milliSeconds
								//stage.listPos--
							switch stage.playerListPos
								case 0
									arrayPos0 = 500
									break
								case 1
									arrayPos0 = 530
									break
								case 2
									arrayPos0 = 560
									break
								case 5
									arrayPos0 = 470
									break		
								case 6
									arrayPos0 = 3700
									break						
								case 7
									arrayPos0 = 3750
									break						
								case 8
									arrayPos0 = 3790
									break						
								case 9
									arrayPos0 = 3830
									break													
							end switch
							if SuperTimeAttack == 1
								arrayPos0 = 590
							end if
							arraypos0 += stage.listPos
							if timeAttack.result < saveRAM[arrayPos0]
								saveRAM[arrayPos0] = timeAttack.result
								//object[9].value2 = timeAttack.result
								PlaySfx(SfxName[Event], 0)
							end if
							WriteSaveRAM()
						end if
					end if
					stage.timeEnabled = 0
					object.state = 1
					PlaySfx(SfxName[Sign Post], 0)
					if PlayerObject_SuperState == 1
						PlayerObject_SuperState = 2
					end if
				end if
			end if
		end if
		break
	case 1
		music.volume--
		object.value1++
		if object.value1 == 2
			object.value1 = 0
			object.frame++
			if object.frame == 5
				object.frame = 1
				Rand(temp0, 48)
				temp0 <<= 16
				temp0 += object.xpos
				temp0 -= 0x180000
				Rand(temp1, 32)
				temp1 <<= 16
				temp1 += object.ypos
				temp1 -= 0x140000
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
				object.value0--
				if object.value0 == 0
					if object[0].type == TypeName[Blank Object]
						object.state = 4
						if object.propertyValue < 3
							PlayMusic(1)
						else
							if object.outOfBounds == 0
								PlayMusic(1)
							end if
						end if
						object[30].type = TypeName[Act Finish]
						object[30].drawOrder = 6
						object[30].priority = PRIORITY_ACTIVE
						object[0].controlMode = -1
						object[0].interaction = 0
						if stage.player2Enabled == 1
							object[1].interaction = 0
							object[1].controlMode = -1
							object[1].up = 0
							object[1].down = 0
							object[1].left = 0
							object[1].jumpPress = 0
							object[1].jumpHold = 0
						end if
					else
						object[0].controlMode = -1
						//object[0].interaction = 0
						object[0].value15 = 0
						object[0].up = 0
						object[0].down = 0
						object[0].left = 0
						object[0].jumpPress = 0
						object[0].jumpHold = 0
						if stage.player2Enabled == 1
							object[1].controlMode = -1
							object[1].interaction = 0
							object[1].up = 0
							object[1].down = 0
							object[1].left = 0
							object[1].jumpPress = 0
							object[1].jumpHold = 0
						end if
						object.state = 2
					end if
				end if
			end if
		end if
		temp0 = 40
		temp0 += screen.xcenter
		temp0 <<= 16
		temp0 += object.xpos
		if object[0].xpos > temp0
			object[0].xpos = temp0
			object[0].speed = 0
			object[0].xvel = 0
		end if
		if stage.player2Enabled == 1
			if object[1].xpos > temp0
				object[1].xpos = temp0
				object[1].speed = 0
				object[1].xvel = 0
			end if
		end if
		break
	case 2
		if object[0].type == TypeName[Blank Object]
			object.state = 4
			if object.propertyValue < 3
				PlayMusic(1)
			else
				if object.outOfBounds == 0
					PlayMusic(1)
				end if
			end if
			object[30].type = TypeName[Act Finish]
			object[30].drawOrder = 6
			object[30].priority = PRIORITY_ACTIVE
		end if
		if object[0].controlMode == -1
			object[0].right = 1
			if object[0].xvel < 0
				object[0].interaction = 1
			else
				object[0].interaction = 0
			end if
		end if
		if stage.player2Enabled == 1
			object[1].controlMode = object[0].controlMode
			if object[1].controlMode == -1
				if object[1].left == 0
					object[1].right = 1
				end if
			end if
		end if
		temp0 = screen.xcenter
		temp0 <<= 16
		temp0 += object.xpos
		if object[0].xpos > temp0
			if object.propertyValue < 3
				PlayMusic(1)
			else
				if object.outOfBounds == 0
					PlayMusic(1)
				end if
			end if
			signpost.timeout = 0
			object.state = 3
			object[30].type = TypeName[Act Finish]
			object[30].drawOrder = 6
			object[30].priority = PRIORITY_ACTIVE
		else
			signpost.timeout++
			if signpost.timeout >= 300
				if object.propertyValue < 3
					PlayMusic(1)
				else
					if object.outOfBounds == 0
						PlayMusic(1)
					end if
				end if
				signpost.timeout = 0
				object.state = 3
				object[30].type = TypeName[Act Finish]
				object[30].drawOrder = 6
				object[30].priority = PRIORITY_ACTIVE
			end if
		end if
		break
	case 3
		if object[0].controlMode == -1
			object[0].right = 1
			if object[0].xvel < 0
				object[0].interaction = 1
			else
				object[0].interaction = 0
			end if
		end if
		temp0 = 40
		temp0 += screen.xcenter
		temp0 <<= 16
		temp0 += object.xpos
		if object[0].xpos > temp0
			object[0].xpos = temp0
			object[0].speed = 0
			object[0].xvel = 0
		end if
		if stage.player2Enabled == 1
			if object[1].controlMode == -1
				object[1].right = 1
			end if
			if object[1].xpos > temp0
				object[1].xpos = temp0
				object[1].speed = 0
				object[1].xvel = 0
			end if
		end if
		break
	case 4
		if stage.player2Enabled == 1
			temp0 = 40
			temp0 += screen.xcenter
			temp0 <<= 16
			temp0 += object.xpos
			object[1].right = 1
			object[1].controlMode = -1
			if object[1].xpos > temp0
				object[1].xpos = temp0
				object[1].speed = 0
				object[1].xvel = 0
			end if
		end if
		break
	end switch
end event


event ObjectDraw
	DrawSprite(0)
	if object.value0 > 8
		DrawSprite(object.frame)
	else
		temp0 = object.frame
		temp0 += 4
		DrawSprite(temp0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-24, 16, 48, 16, 34, 214)
	switch stage.playerListPos
	case 0
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 1, 150)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 1, 183)
		SpriteFrame(-24, -16, 48, 32, 34, 83)
		SpriteFrame(-16, -16, 32, 32, 1, 150)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 1, 183)
		break
	case 1
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 83, 131)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 83, 163)
		SpriteFrame(-24, -16, 48, 32, 34, 116)
		SpriteFrame(-16, -16, 32, 32, 83, 131)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 83, 163)
		break
	case 2
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 83, 256)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 116, 256)
		SpriteFrame(-24, -16, 48, 32, 34, 149)
		SpriteFrame(-16, -16, 32, 32, 83, 256)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 116, 256)
		break
	case 5
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 149, 256)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 182, 256)
		SpriteFrame(-24, -16, 48, 32, 34, 256)
		SpriteFrame(-16, -16, 32, 32, 149, 256)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 182, 256)
		break
	case 6
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 387, 34)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 420, 34)
		SpriteFrame(-24, -16, 48, 32, 402, 1)
		SpriteFrame(-16, -16, 32, 32, 387, 34)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 420, 34)
		break
	case 7
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 255, 34)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 288, 34)
		SpriteFrame(-24, -16, 48, 32, 255, 1)
		SpriteFrame(-16, -16, 32, 32, 255, 34)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 288, 34)
		break
	case 8
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 256, 67)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 289, 67)
		SpriteFrame(-24, -16, 48, 32, 353, 1)
		SpriteFrame(-16, -16, 32, 32, 256, 67)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 289, 67)
		break
	case 9
		SpriteFrame(-24, -16, 48, 32, 34, 182)
		SpriteFrame(-16, -16, 32, 32, 321, 34)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 354, 34)
		SpriteFrame(-24, -16, 48, 32, 304, 1)
		SpriteFrame(-16, -16, 32, 32, 321, 34)
		SpriteFrame(-4, -16, 8, 32, 189, 131)
		SpriteFrame(-16, -16, 32, 32, 354, 34)
		break
	end switch
	foreach (TypeName[Sign Post], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].frame = 1
		object[arrayPos0].value0 = 16
		object[arrayPos0].priority = PRIORITY_XBOUNDS
	next
	SetTableValue(TypeName[Sign Post], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SignPost_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SignPost_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-24, 16, 48, 16, 34, 214)
	SpriteFrame(-24, -16, 48, 32, 34, 182)
end event
